#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sqlite3.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define  N  64
#define  R  1   //  user register
#define  L  2   //  user login
#define  Q  3   //  query word
#define  A  4   //  history record
#define  M  5
#define  D  6

#define UMASK 1234

typedef struct {
	int root;
	int  type; // 
	char  name[N];  // 姓名	
	char  password[N]; // 密码
	char addr[N]; // 地址	
	int age; // 年龄	
	int level; // 等级	
	int no; //编号	
	char phone[N]; // 电话	
	double salary ; // 工资
	char data[256];
}USER_INFO;

int do_register(int socketfd, USER_INFO *msg);
int do_add(int socketfd, USER_INFO* msg);
int do_u_modify(int socketfd,USER_INFO *msg);
int do_modify(int socketfd,USER_INFO *msg);
int do_login(int socketfd, USER_INFO *msg);
int do_query(int socketfd, USER_INFO *msg);
int do_delete(int socketfd,USER_INFO *msg);

int main(int argc, char *argv[])
{
	int socketfd ;
	struct sockaddr_in server_addr;
	USER_INFO msg;

	if (argc < 3)
	{
		printf("Usage : %s <serv_ip> <serv_port>\n", argv[0]);
		exit(-1);
	}
	if ((socketfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
	{
		perror("fail to socket");
		exit(-1);
	}
	bzero(&server_addr, sizeof(server_addr));
	server_addr.sin_family = PF_INET;
	server_addr.sin_addr.s_addr = inet_addr(argv[1]);
	server_addr.sin_port = htons(atoi(argv[2]));
	if (connect(socketfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
	{
		perror("fail to connect");
		exit(-1);
	}
	int n;
	while(1)
	{
		int ret = 0;
		printf("************************************\n");
		printf("* 1: register   2: login   3: quit *\n");
		printf("************************************\n");
		printf("please choose : ");

		if(scanf("%d", &n) <= 0)
		{
			perror("scanf");
			exit(-1);
		}

		switch(n)
		{
			case 1:
				do_register(socketfd, &msg);
				break;
			case 2:
				ret = do_login(socketfd, &msg);
				if((ret)&& (msg.root == 1))
					goto admin;
				else if((ret) == 1 && msg.root == 0)
					goto user;
				break;
			case 3:
				close(socketfd);
				exit(0);
				break;

		}
	}
admin:
	while(1)
	{
		printf("********************************************************\n");
		printf("* 1:adduser  2:deluser  3:modify  4:query  5:quit *\n");
		printf("********************************************************\n");
		printf("please choose : ");

		if(scanf("%d", &n) <= 0)
		{
			perror("scanf");
			exit(-1);
		}
		if(n==5)
		{
			break;
		}
		switch(n)
		{
			case 1:
				do_add(socketfd, &msg);
				break;
			case 2:
				do_delete(socketfd, &msg);
				break;
			case 3:
				do_modify(socketfd, &msg);
				break;
			case 4:
				do_query(socketfd, &msg);
				break;
		
		}
	}

user:
	while(1)
	{
		printf("************************************\n");
		printf("* 1: query   2: modify   3: quit *\n");
		printf("************************************\n");
		printf("please choose : ");

		if(scanf("%d", &n) <= 0)
		{
			perror("scanf");
			exit(-1);
		}

		if(n==3)
		{
			break;
		}
		switch(n)
		{
			case 1:
				do_query(socketfd, &msg);
				break;
			case 2:
				do_u_modify(socketfd, &msg);
				break;
				
		}
	}

	return 0;
}

int do_register(int socketfd, USER_INFO *msg)   //管理员注册
{
	int s;
	printf("please input Adm umask");
	scanf("%d", &s);
	if(s != UMASK)
	{
		return 0;
	}
	msg->type = R;
	printf("input your name:");					
	scanf("%s", msg->name);						//输入管理员名
	printf("input your password:");
	scanf("%s", msg->password);	//输入密码
	msg->root = 1;
	/*printf("retype your password");
	scanf("%s",msg->password);
	printf("adm register?enter y/n to chose");
	scanf("%s",s);
	if(strncmp(s,"y",1)!=0)//user register
	{
		send(socketfd, msg, sizeof(USER_INFO), 0);	
		recv(socketfd, msg, sizeof(USER_INFO), 0);  
	}
	else{
		printf("enter Adm umask:");
		scanf("%s\n",msg->root);//设置权限
		send(socketfd, msg, sizeof(USER_INFO), 0);	
		recv(socketfd, msg, sizeof(USER_INFO), 0);  
	}*/
	send(socketfd, msg, sizeof(USER_INFO), 0);	
	recv(socketfd, msg, sizeof(USER_INFO), 0); 
	printf("%s has registered %s\n", msg->name, msg->data);
	return 0;
}

int do_login(int socketfd, USER_INFO *msg)
{	
	msg->type = 2;

	printf("login name:");
	scanf("%s", msg->name);

	printf("login password:");
	scanf("%s", msg->password);
	getchar();

	send(socketfd, msg, sizeof(USER_INFO), 0);
	
	recv(socketfd, msg, sizeof(USER_INFO), 0);
	printf("msg->root = %d\n", msg->root);
	if(strncmp(msg->data, "OK", 2) == 0)
	{
		printf("login: ok\n");
		return 1;
	}
	printf("login: %s\n", msg->data);
	return 0;
}

int do_add(int socketfd, USER_INFO* msg)
{
	
	msg->type = A;
	msg->root = 0;
	printf("please input name:");
	scanf("%s", msg->name);
	
	
	printf("please input password:");
	scanf("%s",msg->password);
	getchar();
	
	printf("please input address:");
	scanf("%s",msg->addr);
	getchar();

	printf("please input age:");
	scanf("%d", &msg->age);
	getchar();
	
	printf("please input level:");
	scanf("%d", &msg->level);
	getchar();
	
	printf("please input no:");
	scanf("%d", &msg->no);
	getchar();
	
	printf("please input phone:");
	scanf("%s", msg->phone);
	getchar();
	
	printf("please input level:");
	scanf("%lf", &msg->salary);
	getchar();	
	
	send(socketfd, msg, sizeof(USER_INFO), 0);
	recv(socketfd, msg, sizeof(USER_INFO), 0);

	if(strncmp(msg->data, "OK", 2) == 0)
	{
		printf("add user : ok\n");
		return 1;
	}
	printf("add user: %s\n", msg->data);
	return 0;
	
	
}
int do_delete(int socketfd,USER_INFO *msg)
{
	msg->type = D;
	
	printf("please enter employee's no: ");
	scanf("%d", &msg->no);
	getchar();

	send(socketfd, msg, sizeof(USER_INFO), 0);
	recv(socketfd, msg, sizeof(USER_INFO), 0);
	printf("del user: %s\n",msg->data);

	return 0;
}
int do_query(int socketfd, USER_INFO *msg)
{
	msg->type = Q;

	printf("enter employee's name: ");
	scanf("%s", msg->name);
	getchar();
	/*printf("please enter employee's no");
	scanf("%s",msg->no);
	getchar();
	printf("enter employee's name");
	scanf("%s",msg->name);
	getchar();
	printf("enter employee's password");
	scanf("%s",msg->password);
	getchar();*/
	send(socketfd, msg, sizeof(USER_INFO), 0);

	recv(socketfd, msg, sizeof(USER_INFO), 0);
	recv(socketfd, msg, sizeof(USER_INFO), 0);
	printf("second %s\n",msg->data);

	return 0;		
}
int do_u_modify(int socketfd,USER_INFO *msg)
{
	msg->type = M;
/*
	printf("enter your name:");
	scanf("%s",msg->name);
	printf("enter your password:");
	scanf("%s",msg->password);
    send(socketfd, msg, sizeof(USER_INFO), 0);
	recv(socketfd, msg, sizeof(USER_INFO), 0);
	printf("%s",msg->data);
*/
	while(1)
	{
		int n;
		printf("**************************************\n");
		printf("1.password 2.telphone 3.address 4.quit\n");
		printf("**************************************\n");
		scanf("%d", &n);
		getchar();
		if(n==4)
		{
			break;
		}
		switch (n)
		{
		case 1:
			printf("please input new password");
			scanf("%s",msg->password);
			getchar();
			break;
		case 2:
			printf("please input your new telphone");
			scanf("%s",msg->phone);
			getchar();
			break;
		case 3:
			printf("please input your new address");
			scanf("%s",msg->addr);
			getchar();
			break;
		}
	}
    send(socketfd, msg, sizeof(USER_INFO), 0);
	recv(socketfd, msg, sizeof(USER_INFO), 0);
	printf("%s",msg->data);
	return 0;
}
int do_modify(int socketfd, USER_INFO *msg)
{
	msg->type = M;
	printf("the type is:%d\n",msg->type);
	printf("enter a number: ");
	scanf("%d", &msg->no);

	send(socketfd, msg, sizeof(USER_INFO), 0);

	recv(socketfd, msg, sizeof(USER_INFO), 0);

	printf("root:%d,name:%s,password: %s,addr:%s,age:%d,level:%d,no:%d,phone:%s,salary:%lf",
			msg->root,msg->name,msg->password,msg->addr,msg->age,msg->level,msg->no,msg->phone,msg->salary);
	msg->type = M;
	
	recv(socketfd, msg, sizeof(USER_INFO), 0);

	printf("root:%d,name:%s,password: %s,addr:%s,age:%d,level:%d,no:%d,phone:%s,salary:%lf",
			msg->root,msg->name,msg->password,msg->addr,msg->age,msg->level,msg->no,msg->phone,msg->salary);

	/*printf("enter your name:");
	scanf("%s",msg->name);
	printf("enter your password:");
	scanf("%s",msg->password);
    send(socketfd, msg, sizeof(USER_INFO), 0);	
	recv(socketfd, msg, sizeof(USER_INFO), 0);
	printf("%s",msg->data);*/
	while(1)
	{
		int n;
		printf("**********************************\n");
		printf("1.password 2.salary 3.level 4.quit\n");
		printf("**********************************\n");
		printf("Input your chose: ");
		scanf("%d", &n);
		if(4 == n) {
			break;
		}
		switch (n)
		{
		case 1:
			printf("please input new password: ");
			scanf("%s",msg->password);
			getchar();
			printf("please input your salary: ");
			scanf("%lf",&msg->salary);
			getchar();
			break;
		case 3:
			printf("please input your new level: ");
			scanf("%d", &msg->level);
			getchar();
			break;
		case 4:
			return 0;
			break;
		}
	}
	printf("name:%s,password:%s, type:%d\n",msg->name,msg->password,msg->type);
    send(socketfd, msg, sizeof(USER_INFO), 0);
	recv(socketfd, msg, sizeof(USER_INFO), 0);
	printf("%s",msg->data);
	return 0;
}





